package com.UnionPayCenter.account.manager.impl;

import com.UnionPayCenter.account.entity.FundsBill;
import com.UnionPayCenter.account.entity.PayBill;
import com.UnionPayCenter.account.enums.BillDutyEnum;
import com.UnionPayCenter.account.enums.FundsBillTypeEnum;
import com.UnionPayCenter.account.enums.PayChannelEnum;
import com.UnionPayCenter.account.enums.PayStatusEnum;
import com.UnionPayCenter.account.manager.PayBillManager;
import com.UnionPayCenter.account.service.FundsBillService;
import com.UnionPayCenter.entity.ShopInfoEntity;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.PayBillMapper;
import com.UnionPayCenter.mapper.ShopInfoMapper;
import com.UnionPayCenter.service.ServiceTemplate;
import com.UnionPayCenter.util.Asserts;
import com.UnionPayCenter.util.IdGen;
import com.github.pagehelper.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class PayBillManagerImpl implements PayBillManager {

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private FundsBillService fundsBillService;

    @Autowired
    private ServiceTemplate serviceTemplate;

    @Autowired
    private ShopInfoMapper shopInfoMapper;

    @Override
    public String savePayBill(PayBill payBill) {
        Asserts.notNull(payBill, "支付记录");
        Asserts.naturalNumber(payBill.getPayAccountId(), "支付账户id");
        Asserts.naturalNumber(payBill.getAmount(), "支付金额");
        //Asserts.notNull(payBill.getState(), "支付状态");
        payBill.setState(PayStatusEnum.SUCCESS);
        payBill.setSn(IdGen.newStringId());
        payBill.setCreateTime(new Date());
        payBill.setUpdateTime(new Date());
        if (payBill.getPayChannel() == null) {
            payBill.setPayChannel(PayChannelEnum.UNION_PAY);
        } else {
            // 校验支付渠道是否合法
            PayChannelEnum.getEnum(payBill.getPayChannel().code());
        }
        payBillMapper.insert(payBill);


        FundsBill fundsBill = new FundsBill();
        BeanUtils.copyProperties(payBill, fundsBill);
        fundsBill.setBillDuty(BillDutyEnum.PAY);
        fundsBill.setBillType(FundsBillTypeEnum.PAY);
        fundsBillService.saveFundsBill(fundsBill);

        return payBill.getSn();
    }

    @Override
    public Page<PayBill> getPayBillByCondition(PayBill payBill, Integer page, Integer pageSize) {
        Asserts.notNull(payBill, "查询条件");
        return getPayBill(payBill, payBill.getState(), page, pageSize);
    }

    @Override
    public Page<PayBill> getPayBillByUserId(Integer userId, PayStatusEnum state, Integer page, Integer pageSize) {
        Asserts.naturalNumber(userId, "用户Id");
        PayBill payBill = new PayBill();
        payBill.setUserId(userId);
        return getPayBill(payBill, state, page, pageSize);
    }

    @Override
    public Page<PayBill> getPayBillByShopId(Integer shopId, PayStatusEnum state, Integer page, Integer pageSize) {
        Asserts.naturalNumber(shopId, "店铺Id");
        ShopInfoEntity shopInfo = shopInfoMapper.selectByShop(shopId);
        //  从店铺中查找商户Id
        if (shopInfo == null) {
            throw new ApiException(ErrorCodeEnum.TK1010);
        }
        PayBill payBill = new PayBill();
        payBill.setPayeeMerchantId(shopInfo.getMerchantId());
        return getPayBill(payBill, state, page, pageSize);
    }

    /**
     * 根据条件查询支付流水
     *
     * @param payBill  查询条件
     * @param state    状态
     * @param page     页码
     * @param pageSize 每页数据量
     * @return 支付流水，每页形式返回
     */
    private Page<PayBill> getPayBill(PayBill payBill, PayStatusEnum state, Integer page, Integer pageSize) {
        if (state != null) {
            payBill.setState(state);
        }
        return serviceTemplate.pagination(page, pageSize, () -> payBillMapper.selectByCondition(payBill));
    }

}
